feat: disable product cookie when no targeting is true#1071
Conversation
|
|
||
| it('should update Products localStorage when noTargeting is false by default', () => { | ||
| const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; | ||
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); |
There was a problem hiding this comment.
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); | |
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } }); |
| it('should NOT update Products localStorage when noTargeting is true', () => { | ||
| mpInstance._Store.setNoTargeting(true); | ||
| const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; | ||
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); |
There was a problem hiding this comment.
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); | |
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } }); |
| it('should update Products localStorage when noTargeting is false', () => { | ||
| mpInstance._Store.setNoTargeting(false); | ||
| const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; | ||
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); |
There was a problem hiding this comment.
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as any); | |
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } }); |
| this.setCartProducts = function(allProducts) { | ||
| if (!mpInstance._Store.isLocalStorageAvailable) { | ||
| if ( | ||
| !mpInstance._Store.isLocalStorageAvailable && |
There was a problem hiding this comment.
This should be OR, not AND. If local storage isn't available, or if getNoTargeting returns true, then don't set the cart products
| !mpInstance._Store.isLocalStorageAvailable && | |
| !mpInstance._Store.isLocalStorageAvailable || mpInstance._Store.getNoTargeting |
| mParticle._resetForTests(MPConfig); | ||
| }); | ||
|
|
||
| it('should update Products localStorage when noTargeting is false by default', () => { |
There was a problem hiding this comment.
| it('should update Products localStorage when noTargeting is false by default', () => { | |
| it('should save products to localStorage when noTargeting is false by default', () => { |
| expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); | ||
| }); | ||
|
|
||
| it('should NOT update Products localStorage when noTargeting is true', () => { |
There was a problem hiding this comment.
| it('should NOT update Products localStorage when noTargeting is true', () => { | |
| it('should NOT save products to localStorage when noTargeting is true', () => { |
| expect(mpInstance._Persistence.getCartProducts(testMPID)).toEqual([]); | ||
| }); | ||
|
|
||
| it('should update Products localStorage when noTargeting is false', () => { |
There was a problem hiding this comment.
| it('should update Products localStorage when noTargeting is false', () => { | |
| it('should save products to localStorage when noTargeting is false', () => { |
| }); | ||
|
|
||
| it('should not save products LS when noTargeting is true', async () => { | ||
| mParticle.config.noTargeting = true; |
There was a problem hiding this comment.
I'm surprised this is working.
Per the docs, this should be:
| mParticle.config.noTargeting = true; | |
| mParticle.config.launcherOptions = {noTargeting: true}; |
There was a problem hiding this comment.
@rmi22186 Currently noFunctional / noTargeting is only read from top‑level mParticle.config during init, not from config.launcherOptions that is why mParticle.config.noTargeting = true; works. To use mParticle.config.launcherOptions = {noTargeting: true}; we need to update store to source privacy flags from config.launcherOptions.
There was a problem hiding this comment.
Now that you have updated the noTargeting and noFunctional to pull from config.launcherOptions, this will need to be updated
| mParticle._resetForTests(MPConfig); | ||
| }); | ||
|
|
||
| it('should not save products LS when noTargeting is true', async () => { |
There was a problem hiding this comment.
| it('should not save products LS when noTargeting is true', async () => { | |
| it('should not save products to LS when noTargeting is true', async () => { |
| expect(localStorageProducts).to.not.be.ok; | ||
| }); | ||
|
|
||
| it('should save products LS when noTargeting is false', async () => { |
There was a problem hiding this comment.
| it('should save products LS when noTargeting is false', async () => { | |
| it('should save products to LS when noTargeting is false', async () => { |
| }); | ||
|
|
||
| it('should save products LS when noTargeting is false', async () => { | ||
| mParticle.config.noTargeting = false; |
There was a problem hiding this comment.
| mParticle.config.noTargeting = false; | |
| mParticle.config.launcherOptions = {noTargeting: false}; |
| expect(localStorageProducts).to.be.ok; | ||
| }); | ||
|
|
||
| it('should save products LS when noTargeting is false by default', async () => { |
There was a problem hiding this comment.
| it('should save products LS when noTargeting is false by default', async () => { | |
| it('should save products to LS when noTargeting is false by default', async () => { |
…isable-product-cookie-when-noTargeting-is-true
…isable-product-cookie-when-noTargeting-is-true
rmi22186
left a comment
There was a problem hiding this comment.
There are changes to persistnece.js in both
https://github.com/mParticle/mparticle-web-sdk/pull/1070/files and
https://github.com/mParticle/mparticle-web-sdk/pull/1071/files
I feel like it's going to create a merging/rebasing nightmare once 1070 is merged, or 1071 is merged first. I think you need to either combine 1070 and 1071 into a single PR, or use 1070 as what you are PR-ing into instead of development for this branch.
I agree! I have changed the target for PR to 1070. Once this PR is approved I'll merge it into 1070 and then:
|
| if (mpInstance._Store.getPrivacyFlag('Products')) { | ||
| return; | ||
| } | ||
| if (mpid) { | ||
| allLocalStorageProducts = allLocalStorageProducts || {}; | ||
| allLocalStorageProducts[mpid] = currentUserProducts; | ||
| try { | ||
| window.localStorage.setItem( | ||
| encodeURIComponent(mpInstance._Store.prodStorageName), | ||
| Base64.encode(JSON.stringify(allLocalStorageProducts)) | ||
| ); | ||
| if (!mpInstance._Store.getPrivacyFlag('Products')) { | ||
| window.localStorage.setItem( | ||
| encodeURIComponent(mpInstance._Store.prodStorageName), | ||
| Base64.encode(JSON.stringify(allLocalStorageProducts)) | ||
| ); | ||
| } |
There was a problem hiding this comment.
Is the if check for 368 necessary here? Since you have 361-363 already returning based on the Products privacy flag, I don't think you need to check for the inverse of 361 on line 368.
There was a problem hiding this comment.
I agree, its not needed here since we're already returning at line 362
| window.localStorage.setItem( | ||
| encodeURIComponent(mpInstance._Store.prodStorageName), | ||
| Base64.encode(JSON.stringify(allLocalStorageProducts)) | ||
| ); |
There was a problem hiding this comment.
I would also clean up the original code a bit so that i's more readable:
| window.localStorage.setItem( | |
| encodeURIComponent(mpInstance._Store.prodStorageName), | |
| Base64.encode(JSON.stringify(allLocalStorageProducts)) | |
| ); | |
| const encodedKey = encodeURIComponent(mpInstance._Store.prodStorageName); | |
| const encodedValue = Base64.encode(JSON.stringify(allLocalStorageProducts)); | |
| window.localStorage.setItem(encodedKey, encodedValue); |
| const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; | ||
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); | ||
| expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); |
There was a problem hiding this comment.
I think instead of trying to coerce the Type, let's see if we can use a Partial so we can maintain type safety.
| const product = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; | |
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } } as unknown as Product[]); | |
| expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); | |
| const product: Partial<Product> = { Name: 'iphone', Sku: 'iphonesku', Price: 599, Quantity: 1 }; | |
| mpInstance._Persistence.setCartProducts({ [testMPID]: { cp: [product] } }); | |
| expect(mpInstance._Persistence.getCartProducts(testMPID).length).toBe(1); |
There was a problem hiding this comment.
@alexs-mparticle I’ve incorporated Partial<Product> for the item, but I’m still hitting the TypeScript error since setCartProducts is typed as Product[] while the runtime path expects a per‑MPID map. That’s why I had to coerce the call to match the actual shape earlier as well. If there’s a better approach or a preferred workaround to avoid the cast while keeping the test on the real write path, please advise and I’ll update.
|
|
Closing this PR since we are removing setCartProducts and getCartProducts |



Instructions
developmentSummary
Updated persistence logic for products local storage:
Added jest tests and integration tests
Testing Plan
Reference Issue (For mParticle employees only. Ignore if you are an outside contributor)